package com.shop2cn.iapi.sqoptrade.testcase;

import java.math.BigDecimal;

import org.joda.time.DateTime;
import org.junit.*;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.parameter.req.DelayReceiveReq;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.shop2cn.iapi.sqoptrade.service.DelayReceiveCall;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.SQPayGateWayCallService;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.database.sharding.db.sharding.model.AutoReceiveOptPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.constant.ECode;

/**
 * 延长收货时间 FreeMaker Template自动生成代码
 */
@TestSuite
@Ignore
public class Ts_SqDelayReceive {

	private static DelayReceiveReq delayReceiveReq;
	private static DelayReceiveCall delayReceiveCall;
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	
	private static long autotest_goodsOwnerId = Long.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("延长收货时间");
	}

	@Before
	public void caseUp() {

		delayReceiveReq = new DelayReceiveReq();
		delayReceiveCall = new DelayReceiveCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(delayReceiveCall.getOpurl());
		orderShardingWapper.activeDeadCommunicationsLink(orderShardingWapper);
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_DelayReceive_003() {
		Logger.start(false, "此订单不存在");
		try {
			int orderId = 1111;
			int sellerId = 288484;
			int delayDay = 1;
			delayReceiveReq.setDelayDay(delayDay);
			delayReceiveReq.setOrderId(orderId);
			delayReceiveReq.setOperateUserId(sellerId);

			// 发送请求
			delayReceiveCall.setData(delayReceiveReq);
			delayReceiveCall.callService();
			Logger.verifyEquals(ECode.FAILED201, delayReceiveCall.getCode(),
					"验证Code");
			Logger.verifyIsContains(ECode.FAILED201_MSG_11,
					delayReceiveCall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_DelayReceive_005() {
		Logger.start(false, "订单号为null");
		try {
			int sellerId = 288484;
			int delayDay = 1;
			delayReceiveReq.setDelayDay(delayDay);
			//delayReceiveReq.setOrderId(orderId);
			delayReceiveReq.setOperateUserId(sellerId);

			// 发送请求
			delayReceiveCall.setData(delayReceiveReq);
			delayReceiveCall.callService();
			Logger.verifyEquals(ECode.ERROR400, delayReceiveCall.getCode(),
					"验证Code");
			Logger.verifyIsContains(ECode.ERROR400_MSG_9,
					delayReceiveCall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_DelayReceive_006() {
		Logger.start(false, "延长的时间为0");
		try {
			int orderId = 1234;
			int sellerId = 288484;
			int delayDay = 0;
			delayReceiveReq.setDelayDay(delayDay);

			delayReceiveReq.setOrderId(orderId);
			delayReceiveReq.setOperateUserId(sellerId);

			// 发送请求
			delayReceiveCall.setData(delayReceiveReq);
			delayReceiveCall.callService();
			Logger.verifyEquals(ECode.ERROR400, delayReceiveCall.getCode(),
					"验证Code");
			Logger.verifyIsContains("必须大于0", delayReceiveCall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_DelayReceive_007() {
		Logger.start(false, "延长的时间小于0");
		try {
			int orderId = 1234;
			int sellerId = 288484;
			int delayDay = -1;
			delayReceiveReq.setDelayDay(delayDay);

			delayReceiveReq.setOrderId(orderId);
			delayReceiveReq.setOperateUserId(sellerId);

			// 发送请求
			delayReceiveCall.setData(delayReceiveReq);
			delayReceiveCall.callService();
			Logger.verifyEquals(ECode.ERROR400, delayReceiveCall.getCode(),
					"验证Code");
			Logger.verifyIsContains("必须大于0", delayReceiveCall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_DelayReceive_011() {
		Logger.start(true, "神器订单，买家延长收货时间失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean);

			Long userId = placeOrderResp.getUserId();
			long goodsOwnerId = placeOrderResp.getOrderList().get(0)
					.getOrderPo().getGoodsOwnerId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);

			// 接单
			SqTradingCallService.sellerAcceptOrder(goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 神器订单发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					1L, null);
			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");

			//自动确认收货时间操作表（auto_receive_opt） 
			AutoReceiveOptPo record = new AutoReceiveOptPo();
			record.setOrderId(orderId);
			record.setExpectReceiveTime(DateTime.now().plusDays(-1).toDate());
			record.setFreeze(false);

			//修改预期自动确认收货
			orderShardingWapper.autoReceiveOptMapper
					.updateByPrimaryKeySelective(record);

			Logger.comment("前置数据准备结束");

			int delayDay = 3;

			delayReceiveReq.setDelayDay(delayDay);
			delayReceiveReq.setOrderId(orderId);
			delayReceiveReq.setOperateUserId(userId);

			delayReceiveReq.setOperateUserType(0);
			// 发送请求
			delayReceiveCall.setData(delayReceiveReq);
			delayReceiveCall.callService();
			
			Logger.verifyEquals(ECode.FAILED201, delayReceiveCall.getCode(),
					"验证Code");

			Logger.verifyEquals("105", delayReceiveCall.getBCode(), "验证Code");
			Logger.verifyIsContains(String.format("订单%s,不能做延长收货处理", orderId),
					delayReceiveCall.getMsg(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_DelayReceive_012() {
		Logger.start(true, "神器订单，客服延长收货时间失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean);

			Long userId = placeOrderResp.getUserId();
			long goodsOwnerId = placeOrderResp.getOrderList().get(0)
					.getOrderPo().getGoodsOwnerId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);

			// 接单
			SqTradingCallService.sellerAcceptOrder(goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);

			// 神器订单发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					1L, null);
			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");

			//自动确认收货时间操作表（auto_receive_opt） 
			AutoReceiveOptPo record = new AutoReceiveOptPo();
			record.setOrderId(orderId);
			record.setExpectReceiveTime(DateTime.now().plusDays(-1).toDate());
			record.setFreeze(false);

			//修改预期自动确认收货
			orderShardingWapper.autoReceiveOptMapper
					.updateByPrimaryKeySelective(record);

			Logger.comment("前置数据准备结束");

			int delayDay = 3;

			delayReceiveReq.setDelayDay(delayDay);
			delayReceiveReq.setOrderId(orderId);
			delayReceiveReq.setOperateUserId(-1);
			delayReceiveReq.setOperateUserType(2);

			delayReceiveReq.setOperateUserType(0);
			// 发送请求
			delayReceiveCall.setData(delayReceiveReq);
			delayReceiveCall.callService();
			
			Logger.verifyEquals(ECode.FAILED201, delayReceiveCall.getCode(),
					"验证Code");

			Logger.verifyEquals("105", delayReceiveCall.getBCode(), "验证Code");
			Logger.verifyIsContains(String.format("订单%s,不能做延长收货处理", orderId),
					delayReceiveCall.getMsg(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}